"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库兼容--兼容b库语法
Case Name   : 创建mot表同时创建索引，合理报错
Create At   : 2022.10.14
Owner       : z@wan005
Description :
    1.创建mot表并同时创建索引
    2.清理环境
Expect      :
    1.合理报错
    2.清理环境完成
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Compatibility(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.common = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.tb_name = 'tb_grammar_case0040'
        self.db_name = 'db_grammar_case0040'

    def test_grammar(self):
        self.log.info('----前置条件：设置参数为off并创建兼容b库---')
        self.default_value = self.common.show_param(
            'enable_incremental_checkpoint')
        sql_res = self.pri_sh.execut_db_sql('alter system set '
                                            'enable_incremental_checkpoint'
                                            ' to off;')
        self.log.info(sql_res)
        self.assertIn(self.constant.ALTER_SYSTEM_SUCCESS_MSG, sql_res)
        restart_msg = self.pri_sh.restart_db_cluster()
        self.log.info(restart_msg)
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:建mot表并创建索引;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop foreign table if exists {self.tb_name};" \
                  f"create foreign table {self.tb_name}(id int," \
                  f"uid int not null," \
                  f"index 索引0040 using btree(uid), sex name,salary numeric);"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd,
                                            dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn('ERROR:  syntax error at or near "index"',
                      sql_res, '执行失败' + text)

    def tearDown(self):
        text = '---step2:清理环境;expect:清理环境完成---'
        self.log.info(text)
        drop_cmd = f"drop database if exists {self.db_name};"
        drop_msg = self.pri_sh.execut_db_sql(drop_cmd, dbname='postgres')
        self.log.info(drop_msg)
        alter_cmd = self.pri_sh.execut_db_sql(
            f'alter system set enable_incremental_checkpoint to'
            f' {self.default_value};')
        self.log.info(alter_cmd)
        self.assertIn(self.constant.ALTER_SYSTEM_SUCCESS_MSG, alter_cmd)
        restart_msg = self.pri_sh.restart_db_cluster()
        self.log.info(restart_msg)
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_msg,
                      '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
